package com.hongsuan.pplive.mobileAPI.pay;

import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cnjson.common.web.BaseController;
import com.weixin.pay.entity.WeixinParameter;
import com.weixin.pay.utils.CommonUtil;
import com.weixin.pay.utils.ConfigUtil;
import com.weixin.pay.utils.PayCommonUtil;
import com.weixin.pay.utils.XMLUtil;

@Controller
@RequestMapping("/pay/weixinpay")
public class WeixinPayApiController extends BaseController {

	/**
	 *
	 * <xml> <appid>wx2421b1c4370ec43b</appid> <attach>支付测试</attach>
	 * <body>APP支付测试</body>//商品描述 <mch_id>10000100</mch_id>
	 * <nonce_str>1add1a30ac87aa2db72f57a2375d8fec</nonce_str>
	 * <notify_url>http://wxpay.weixin.qq.com/pub_v2/pay/notify.v2.php
	 * </notify_url> <out_trade_no>1415659990</out_trade_no>
	 * <spbill_create_ip>14.23.150.211</spbill_create_ip>
	 * <total_fee>1</total_fee>//分为单位 <trade_type>APP</trade_type>
	 * <sign>0CB01533B8C1EF103065174F50BCA001</sign> </xml>
	 * 
	 * 
	 */
	@RequestMapping("/post")
	public @ResponseBody String post(HttpServletRequest request, HttpServletResponse response) {

		return "";
	}

	public static String perPay(WeixinParameter info){
		SortedMap<Object, Object> param = new TreeMap<Object, Object>();
		param.put("appid", ConfigUtil.APPID);// ConfigUtil.APPID);//公众账号id
		param.put("mch_id", ConfigUtil.MCH_ID);// 商户号
		param.put("nonce_str", PayCommonUtil.createNoncestr());// 随机字符串
		param.put("out_trade_no", info.getTradeNo());//商户订单号
		param.put("body", info.getComment());
		param.put("total_fee", info.getFee());//单位为分
		param.put("spbill_create_ip", info.getIp());
		param.put("notify_url", ConfigUtil.NOTIFY_URL);//支付成功后回调的action，与JSAPI相同
		param.put("trade_type", "APP");// NATIVE
		
		String sign = PayCommonUtil.createSign("UTF-8", param);
		param.put("sign", sign);
		
		// String sign1 = PayCommonUtil.createSign2("UTF-8",
		// param1,winxinpayinfo.getS_api_key());
		// param.put("sign", sign1);// 签名

		String requestXML = PayCommonUtil.getRequestXml(param);
		String result = CommonUtil.httpsRequest(ConfigUtil.CHECK_ORDER_URL, "POST", requestXML);
		Map<String, String> map = new HashMap<String, String>();
		try {
			map = XMLUtil.doXMLParse(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		String returnCode = map.get("return_code");
		String resultCode = map.get("result_code");
		if (returnCode.equalsIgnoreCase("SUCCESS") && resultCode.equalsIgnoreCase("SUCCESS")) {
			String tradeState = map.get("trade_state");
			if ("SUCCESS".equals(tradeState)) {
				String prepayid=map.get("prepayid");
				
//				req.appId			= json.getString("appid");
//				req.partnerId		= json.getString("partnerid");
//				req.prepayId		= json.getString("prepayid");
//				req.nonceStr		= json.getString("noncestr");
//				req.timeStamp		= json.getString("timestamp");
//				req.packageValue	= json.getString("package");
//				req.sign			= json.getString("sign");
//				req.extData			= "app data"; // optional
				// get input stream.....
				//TODO ourself business!
				//返回给客户端
				//{"appid":"wxb4ba3c02aa476ea1","partnerid":"1305176001","package":"Sign=WXPay","noncestr":"01eab62307f8212ca69c22bf4d267c1f","timestamp":1465836390,"prepayid":"wx2016061400463070da8c4fc00424784837","sign":"0617D46326A4FACBD7C2621909D56869"}
			}
		}
		
		return "";
	}

	/**
	 * 微信支付回调函数
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/payreturn")
	@ResponseBody
	public String payreturn(HttpServletRequest request, HttpServletResponse response) {

		return "";

	}

}
